package com.youan.pangu.security.brower.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.youan.pangu.base.bean.GlobalValue;
import com.youan.pangu.base.bean.result.ErrorResponse;
import com.youan.pangu.base.bean.result.ErrorType;
import com.youan.pangu.base.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description:
 * @author: 辛晟昊
 * @time: 2020/3/8 15:28
 */
public class PanguFailureAuthenticationHandler implements AuthenticationFailureHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalValue.LOOGER_NAME);
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        logger.error("登陆失败!");
        ErrorResponse errorResponse = new ErrorResponse(e.getLocalizedMessage(), ErrorType.NOLOGIN_ERROR);
        ResponseUtil.write(httpServletResponse, objectMapper.writeValueAsString(errorResponse));
        //SimpleUrlAuthenticationFailureHandler
    }
}
